package cn.zl.swagger.config;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.Contact;
import springfox.documentation.service.SecurityScheme;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author zhanglang
 * @Date 2020/7/1
 */
@Configuration
@EnableSwagger2  //开启Swagger2
public class SwaggerConfig {
    @Value("${swagger2.title}")
    private String title;

    @Value("${swagger2.description}")
    private String description;

    @Value("${swagger2.contact.name}")
    private String contactName;

    @Value("${swagger2.contact.url}")
    private String contactUrl;

    @Value("${swagger2.contact.email}")
    private String contactEmail;

    @Value("${swagger2.version}")
    private String version;

    private static final String AUTHENTICATION_TOKEN = "X-Access-Token";

    @Bean
    public Docket docket1(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("A");
    }
    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("B");
    }
    @Bean
    public Docket docket3(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("C");
    }
    //配置了Swagger的Docket的bean实例
    @Bean
    public Docket docket(Environment environment){
        //设置要显示的Swagger环境
        Profiles profiles=Profiles.of("dev","test");
        //获取项目的环境,通过environment.acceptsProfiles判断是否处于自己设定的环境中
        boolean flag = environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("ZL")
                //enable是否启动swagger，如果为false，则在浏览器中不能访问Swagger
                .enable(flag)
                .select()
                //RequestHandlerSelectors配置要扫描接口对的方式
                //basePackage指定要扫描的包
                //any()扫描全部
                //none()不扫描
                //withClassAnnotation扫描类上的注解，参数是一个注解的反射对象
                //withMethodAnnotation扫描方法上的注解
                .apis(RequestHandlerSelectors.basePackage("cn.zl.swagger.controller"))//设置扫描的基本包
                //paths路径过滤
                // .paths(PathSelectors.ant("/zl/**"))
                .build();
    }

    //多接口例子
    @Bean
    public Docket createMeritsRestApi(){
        List<Predicate<RequestHandler>> list = new ArrayList<>();
        Predicate<RequestHandler> appraise = RequestHandlerSelectors.basePackage("com.tempetek.maviki.business.appraise");
        Predicate<RequestHandler> appraisescore = RequestHandlerSelectors.basePackage("com.tempetek.maviki.business.appraisescore");
        Predicate<RequestHandler> appraiseweight = RequestHandlerSelectors.basePackage("com.tempetek.maviki.business.appraiseweight");
        Predicate<RequestHandler> assess = RequestHandlerSelectors.basePackage("com.tempetek.maviki.business.assess");
        Predicate<RequestHandler> item = RequestHandlerSelectors.basePackage("com.tempetek.maviki.business.item");
        Predicate<RequestHandler> standard = RequestHandlerSelectors.basePackage("com.tempetek.maviki.business.standard");
        Predicate<RequestHandler> storehouse = RequestHandlerSelectors.basePackage("com.tempetek.maviki.common.controller");


        list.add(appraise);
        list.add(appraisescore);
        list.add(appraiseweight);
        list.add(assess);
        list.add(item);
        list.add(standard);
        list.add(storehouse);
        Predicate<RequestHandler> apiOperation = RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class);
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("kh")
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .select()
                .apis(Predicates.and(Predicates.or(list), apiOperation))
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(Collections.singletonList(securityScheme()));
    }
    //配置Swagger信息-apiInfo
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title(title).description(description)
                .contact(new Contact(contactName, contactUrl, contactEmail)).version(version).build();
    }
//    private ApiInfo apiInfo(){
//        //作者信息
//        Contact contact = new Contact("ZL","http://www.zllwsy.com","zllwsy@outlook.com");
//        return new ApiInfo(
//                "ZL的SwaggerAPI文档",
//                "学习永无止境",
//                "v1.0",
//                "urn:tos",
//                contact,
//                "Apache 2.0",
//                "http://www.apache.org/licenses/LICENSE-2.0",
//                new ArrayList()
//        );
//    }
    //配置权限头
    @Bean
    public SecurityScheme securityScheme() {
        return new ApiKey(AUTHENTICATION_TOKEN, AUTHENTICATION_TOKEN, "header");
    }
}
